<template>
  <div ref="chart" style="height: 100%;" />
</template>
<script>
import { Chart, registerShape } from '@antv/g2';
import { getTicks } from '@/utils/chartScale.js';
export default {
  name: 'BarChart',
  props: {
    data: { type: Array, required: true },
    x: { type: String, default: 'x' },
    y: { type: String, default: 'y' },
    s: { type: String, default: 's' },
    color: { type: Array, required: true },
    unit: { type: String, required: true },
    text: { type: String, required: true }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    data() {
      this.updateData();
    }
  },
  mounted() {
    this.initChart();
    this.drawChart();
  },
  methods: {
    getTicks,
    initChart() {
      const ref = this.$refs.chart;
      const ratio = window.ratio || 1;
      this.chart = new Chart({
        container: ref,
        width: ref.clientWidth,
        height: ref.clientHeight,
        appendPadding: [22 * ratio, 0, 0, 0]
      });
    },
    drawChart() {
      const ref = this.$refs.chart;
      const ratio = window.ratio || 1;
      const x = this.x;
      const y = this.y;
      const s = this.s;
      const height = 0.06;
      // 自定义图形
      registerShape('interval', 'cuboid', {
        // 1. 定义关键点
        getPoints(cfg) {
          const x = cfg.x;
          const y = cfg.y;
          const y0 = cfg.y0;
          const width = cfg.size;
          return [
            { x: x - width / 2, y: y0 },
            { x: x - width / 2, y: y - height / 2 },
            { x: x, y: y - height },
            { x: x + width / 2, y: y - height / 2 },
            { x: x + width / 2, y: y0 },
            { x: x, y: y }
          ];
        },
        draw(cfg, container) {
          const group = container.addGroup({});
          const points = this.parsePoints(cfg.points);
          // 渐变高度
          const gradientHeight = 1 - Math.abs(cfg.points[2].y / cfg.points[5].y);
          group.addShape('path', {
            attrs: {
              path: [
                ['M', points[0].x, points[0].y],
                ['L', points[1].x, points[1].y],
                ['L', points[2].x, points[2].y],
                ['L', points[3].x, points[3].y],
                ['L', points[5].x, points[5].y],
                ['L', points[1].x, points[1].y]
              ],
              fill: `l(90) 0:${cfg.color} ${gradientHeight}:#99FFFB`
            }
          });
          group.addShape('path', {
            attrs: {
              path: [
                ['M', points[0].x, points[0].y],
                ['L', points[1].x, points[1].y],
                ['L', points[2].x, points[2].y],
                ['L', points[3].x, points[3].y],
                ['L', points[4].x, points[4].y],
                ['Z']
              ],
              fill: `l(90) 0:${cfg.color} 1:${cfg.color}00`
            }
          });
          return group;
        }
      });
      this.chart.data(this.data);
      this.chart.scale(y, {
        min: 0,
        tickCount: 4,
        tickMethod: this.getTicks
      });
      this.chart.tooltip({
        showMarkers: false,
        customItems: items => {
          items.forEach((item, idx) => {
            item.name = this.text[idx];
          });
          return items;
        },
        domStyles: {
          'g2-tooltip-title': {
            fontSize: '.875rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-list-item': {
            fontSize: 0,
            overflow: 'hidden'
          },
          'g2-tooltip-name': {
            fontSize: '.75rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-value': {
            fontSize: '.75rem',
            color: 'rgba(0, 0, 0, 0.6)',
            fontWeight: '780'
          }
        }
      });
      // x轴
      this.chart.axis(x, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular'
          },
          autoHide: false,
          offset: 5 * ratio
        },
        line: null,
        tickLine: null
      });
      // y轴
      this.chart.axis(y, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular'
          },
          offset: 5 * ratio
        },
        grid: {
          line: {
            style: {
              stroke: 'rgba(255, 255, 255, 0.2)',
              lineWidth: 1 * ratio
            }
          }
        }
      });
      this.chart.legend(false);
      // 单位标注
      this.chart.annotation().text({
        content: this.unit,
        position: ['end', 'start'],
        style: {
          fill: '#ADADAD',
          fontSize: 12 * ratio,
          fontFamily: 'Helvetica-regular'
        },
        background: {
          padding: 2 * ratio
        },
        offsetY: -ref.clientHeight + 26 * ratio,
        offsetX: -ref.clientWidth + 3 * ratio
      });
      this.chart
        .interval()
        .position(`${x}*${y}`)
        .shape('cuboid')
        .color(s, this.color)
        .animate({
          appear: {
            animation: 'scale-in-y',
            delay: 0,
            duration: 2000
          }
        });
      this.chart.render();
    },
    // 数据更新
    updateData() {
      this.chart.changeData(this.data);
    }
  }
};
</script>
